﻿using System;
using Basmus.ReadWrite;

namespace Basmus.Values.Attributive.ReadWrite
{
    public class BaseTypeObjectElementsReaderWriterBuilder : IObjectElementsReaderWriterBuilder
    {
        private readonly IBindingBuilder _bindingBuilder;

        public BaseTypeObjectElementsReaderWriterBuilder(IBindingBuilder bindingBuilder)
        {
            _bindingBuilder = bindingBuilder;
        }

        public IElementsReaderWriter<object> Build(ValuesContext valuesContext)
        {
            var elementType = _bindingBuilder.Build(valuesContext).ElementType;
            if (elementType == typeof(Int32))
            {
                var elementsReaderWriterBuilder = new ElementsReaderWriterBuilder<Int32>(new Int32ReaderWriterBuilder());
                var elementsReaderWriter = elementsReaderWriterBuilder.Build(valuesContext);
                var objectElementsReaderWriter = new ObjectElementsReaderWriter<Int32>(elementsReaderWriter);
                return objectElementsReaderWriter;
            }
            if (elementType == typeof(Boolean))
            {
                var elementsReaderWriterBuilder = new ElementsReaderWriterBuilder<Boolean>(new BooleanReaderWriterBuilder());
                var elementsReaderWriter = elementsReaderWriterBuilder.Build(valuesContext);
                var objectElementsReaderWriter = new ObjectElementsReaderWriter<Boolean>(elementsReaderWriter);
                return objectElementsReaderWriter;
            }
            if (elementType == typeof(Guid))
            {
                var elementsReaderWriterBuilder = new ElementsReaderWriterBuilder<Guid>(new GuidReaderWriterBuilder());
                var elementsReaderWriter = elementsReaderWriterBuilder.Build(valuesContext);
                var objectElementsReaderWriter = new ObjectElementsReaderWriter<Guid>(elementsReaderWriter);
                return objectElementsReaderWriter;
            }
            if (elementType==typeof(string))
            {
                var elementsReaderWriterBuilder = new ElementsReaderWriterBuilder<string>(new StringReaderWriterBuilder());
                var elementsReaderWriter = elementsReaderWriterBuilder.Build(valuesContext);
                var objectElementsReaderWriter = new ObjectElementsReaderWriter<string>(elementsReaderWriter);
                return objectElementsReaderWriter;
            }
            return null;
        }
    }
}